package com.oa.modules.approvalsec1.controller;

import com.oa.common.annotation.OperationLog;
import com.oa.common.utils.ExcelUtils;
import com.oa.common.utils.FreemarkerUtil;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.entity.Propulsion;
import com.oa.modules.approvalsec1.service.OperationlogService;
import com.oa.modules.approvalsec1.service.PropulsionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * (Propulsion)表控制层
 *
 * @author makejava
 * @since 2020-11-18 11:03:35
 */
@RestController
@RequestMapping("propulsion")
public class PropulsionController {
    /**
     * 服务对象
     */
    @Resource
    private PropulsionService propulsionService;
    @Autowired
    private OperationlogService operationlogService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Propulsion selectOne(Long id) {
        return this.propulsionService.queryById(id);
    }

    /**
     * 分页查询
     * @param params
     * @return
     */
    @PostMapping(value = "/queryPage")
    @RequiresPermissions("approvalSec1:propulsion:list")
    @OperationLog("并行推进")
    public R queryPage(@RequestParam Map<String, Object> params){
        PageUtils page = propulsionService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 保存日志
     * @param propulsion
     * @return
     */
    @PostMapping(value = "/saveOperationLog")
    public R saveOperationLog(@RequestBody Propulsion propulsion){
        String operation = "并行推进：查看建设单位名称为："+propulsion.getUnitsName()+"，业务名称为："+propulsion.getBusinessName()+"的详情";
        operationlogService.packOperation(operation);
        return R.ok();
    }

    @PostMapping(value = "/export")
    public void export(@RequestBody Propulsion propulsion, HttpServletResponse response){
        List<Propulsion> records = propulsionService.queryAll(propulsion);
        LinkedHashMap<String, String> titleMap = getTitleMap();
        try {
            ExcelUtils.listToExcel(records, titleMap, "并行推进", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * d导出标题栏
     *
     * @return
     */
    private static LinkedHashMap<String, String> getTitleMap() {
        LinkedHashMap<String, String> names = new LinkedHashMap<String, String>();
        //name和operator是ExcelDTO实体类的属性名，姓名表示name列表头显示的名称
        names.put("businessName", "业务名称");
        names.put("unitsName", "建设单位名称");
        names.put("certificateType", "证件类型");
        names.put("certificateCode", "证件代码");
        names.put("projectName", "项目名称");
        names.put("totalInvest", "项目总投资");
        return names;
    }

    @PostMapping(value = "/getDoc")
    public void getDoc(@RequestBody Propulsion propulsion, HttpServletResponse response) {
        List<Propulsion> records = propulsionService.queryAll(propulsion);
        if(records!=null && records.size()>0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("title", "并行推进");
            List<Map<String, Object>> list = new ArrayList<>();
            if (records != null && records.size() > 0) {
                for (int i = 0; i < records.size(); i++) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("businessName", records.get(i).getBusinessName());
                    map.put("unitsName", records.get(i).getUnitsName());
                    map.put("certificateType", records.get(i).getCertificateType());
                    map.put("certificateCode", records.get(i).getCertificateCode());
                    map.put("projectName", records.get(i).getProjectName());
                    map.put("totalInvest", records.get(i).getTotalInvest());
                    list.add(map);
                }
            }
            dataMap.put("list", list);
            String templateName = "propulsion.ftl";
            String filePath = "D:\\freemarker";
            String fileName = "并行推进" + System.currentTimeMillis() + ".doc";
            try {
                FreemarkerUtil.createWord(dataMap, templateName, filePath, fileName, response);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("文件下载失败");
            }
        }
    }
}
